package class1.s2022350143027.controller;

import class1.s2022350143027.model.OrdersModel;
import class1.s2022350143027.service.YzyOrdersService;
import class1.s2022350143027.service.Impl.YzyOrdersServiceImpl;
import class1.s2022350143027.utils.YzyValidateUtil;

import java.time.LocalDateTime;
import java.util.Scanner;


/**
 * 订单管理显示控制
 * <p>
 * (订单管理业务逻辑接口，订单管理页面实现方法)
 * @author yzy
 * @date 2024-05-02
 */
public class OrdersController {

    //声明订单业务逻辑接口
    protected YzyOrdersService ordersService = new YzyOrdersServiceImpl();

    /**
     * 订单管理页面实现
     * <p>
     * "====       0.返回首页    ===="
     * <p>
     * "====       1.订单查询    ===="
     * <p>
     * "====       2.订单录入    ===="
     * <p>
     * "====       3.订单更新    ===="
     * <p>
     * "====       4.订单删除    ===="
     * <p>
     * "====       5.订单总览    ===="
     * @author yzy
     * @date 2024-05-02
     */
    public void o_page() {
        //输入准备
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n========== 库存管理 =========");
            System.out.println("====       0.返回首页    ====");
            System.out.println("====       1.订单查询    ====");
            System.out.println("====       2.订单录入    ====");
            System.out.println("====       3.订单更新    ====");
            System.out.println("====       4.订单删除    ====");
            System.out.println("====       5.订单总览    ====");
            System.out.println("====       6.导出TXT    ====");
            System.out.println("====       5.Mysql存储    ====");
            System.out.print("请输入功能对应选项:");
            String choice_o = scanner.next();

            switch (choice_o) {
                case "0"://返回首页
                    return ;

                case "1"://订单查询
                    System.out.println("请输入查询方式（单个数值）>>> 0（订单编号或客户名）, 1（日期段）:");
                    String order_choice = scanner.next();
                    if (order_choice.equals("0")) {
                        System.out.print("请输入订单号或客户名:");
                        String orderId_or_customerName = scanner.next();

                        if (!orderId_or_customerName.isEmpty()) {
                            ordersService.show(ordersService.getOrderModel(orderId_or_customerName));
                        }else {System.out.println("输入格式错误，请重试！");}
                    }
                    else if (order_choice.equals("1")) {
                        System.out.println("按要求查找订单的相关信息:（ 日期输入格式为2024-04-25 即年-月-日 ）");//输入订单编号或客户名称或按日期段（开始日期-结束日期）查询

                        System.out.print("请输入开始日期:");
                        String start_date = scanner.next();

                        System.out.print("请输入结束日期:");
                        String end_date = scanner.next();

                        if (YzyValidateUtil.checkDate(start_date) && YzyValidateUtil.checkDate(end_date)) {
                            ordersService.show(ordersService.getOrderModel(start_date,end_date));
                        }else {System.out.println("输入格式错误，请重试！");}
                    }
                    else {
                        System.out.println("输入错误，请重试！");
                    }


                    break;

                case "2"://订单录入
                    //建立单个订单对象模型，储存其订单信息
                    OrdersModel orderModel = new OrdersModel();

                    System.out.print("输入客户名:");
                    String order_customerName = scanner.next();

                    System.out.print("输入商品编号:");
                    String order_goodId = scanner.next();

                    System.out.print("输入商品名称:");
                    String order_goodName = scanner.next();

                    System.out.print("输入商品单价:");
                    String order_goodPrice = scanner.next();

                    System.out.print("输入购买数量:");
                    String order_goodQuantity = scanner.next();

                    //满足商品信息要求就写入对应信息
                    if(!order_customerName.isEmpty() &&
                            !order_goodId.isEmpty() &&
                            !order_goodName.isEmpty() &&
                            YzyValidateUtil.checkGood_name(order_goodName) &&
                            YzyValidateUtil.checkPN(order_goodPrice) &&
                            YzyValidateUtil.checkPN(order_goodQuantity)){
                        orderModel.setOrderId(ordersService.getOrdersData().getLength());
                        orderModel.setOrderTime(LocalDateTime.now());
                        orderModel.setOrderCustomerName(order_customerName);
                        orderModel.setOrderGoodId(order_goodId);
                        orderModel.setOrderGoodName(order_goodName);
                        orderModel.setOrderGoodPrice(Integer.parseInt(order_goodPrice));
                        orderModel.setOrderGoodQuantity(Integer.parseInt(order_goodQuantity));
                        orderModel.setOrderTotalPrice(orderModel.getTotalPrice());

                        if (ordersService.save(orderModel)) {
                            ordersService.show(ordersService.getOrdersData().getOrdersDataList());
                            System.out.println(" ");
                        }

                    }
                    //否则重新操作
                    else {
                        System.out.println("录入商品信息错误，请重试！");
                    }

                    break;

                case "3"://更新订单
                    System.out.print("请输入您需要修改的订单的编号:");
                    String lookOrder_id = scanner.next();
                    if(ordersService.update(lookOrder_id)) {
                        System.out.println("更新成功！");
                    }else {
                        System.out.println("没有此订单！");
                    }
                    break;

                case "4"://删除订单
                    System.out.print("请输入您需要删除的订单的编号:");
                    String lookOrder_id1 = scanner.next();
                    if(ordersService.delete(lookOrder_id1)) {
                        System.out.println("删除成功！");
                    }else {
                        System.out.println("没有此订单！");
                    }
                    break;

                case "5"://订单总览
                    ordersService.show(ordersService.getOrdersData().getOrdersDataList());
                    break;

                default://重新输入
                    System.out.println("输入的数字不正确...请重试。");
                    break;
            }
        }
    }

}
